﻿
// cast5Dlg.cpp: 实现文件
//

#include "stdafx.h"
#include "cast5.h"
#include "cast5Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// Ccast5Dlg 对话框



Ccast5Dlg::Ccast5Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_CAST5_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void Ccast5Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(Ccast5Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &Ccast5Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &Ccast5Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &Ccast5Dlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &Ccast5Dlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// Ccast5Dlg 消息处理程序

BOOL Ccast5Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	namedWindow("view", WINDOW_AUTOSIZE);
	HWND hWnd = (HWND)cvGetWindowHandle("view");
	HWND hParent = ::GetParent(hWnd);//父窗口
	::SetParent(hWnd, GetDlgItem(IDC_PIC_STATIC)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);//隐藏原父窗口



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void Ccast5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void Ccast5Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR Ccast5Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


Mat image;
void Ccast5Dlg::OnBnClickedButton1()
{
	CString picPath;   //定义图片路径变量  
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
		OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, NULL, this);   //选择文件对话框  

	if (dlg.DoModal() == IDOK)
	{
		picPath = dlg.GetPathName();  //获取图片路径  
	}
	//CString to string  使用这个方法记得字符集选用“使用多字节字符”，不然会报错  
	string picpath = picPath.GetBuffer(0);

	image = imread(picpath);
	Mat imagedst;
	//以下操作获取图形控件尺寸并以此改变图片尺寸  
	CRect rect;
	GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect);
	//Rect dst(rect.left, rect.top, rect.right, rect.bottom);
	resize(image, imagedst, cv::Size(rect.Width(), rect.Height()));
	imshow("view", imagedst);
}


void Ccast5Dlg::OnBnClickedButton2()
{
	
	Mat img_canny(image.size(), CV_8UC1, Scalar(0));
	Canny(image, img_canny, 100, 300, 3, true);
	Mat imagedst;
	CRect rect;
	GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect);
	resize(img_canny, imagedst, cv::Size(rect.Width(), rect.Height()));
	imshow("view", imagedst);


}


void Ccast5Dlg::OnBnClickedButton3()
{
	Mat out;
	GaussianBlur(image, out, Size(7, 7), 0, 0);
	Mat imagedst;
	CRect rect;
	GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect);
	resize(out, imagedst, cv::Size(rect.Width(), rect.Height()));
	imshow("view", imagedst);
}


void Ccast5Dlg::OnBnClickedButton4()
{
	Mat dest;
	dest = Mat::zeros(image.size(), image.type());
	int width = image.cols;
	int height = image.rows;
	int channels = image.channels();

	int alphe = 1.8; //(alphe > 1)
	int beta = -30;// 负数对比度越高
	Mat m1;
	image.convertTo(m1, CV_32F); //将原始图片数据（CV_8U类型）转换成CV_32类型，以提高操作的精度
	for (int row = 0; row < height; row++) {
		for (int col = 0; col < width; col++) {
			if (channels == 3) { //对于3通道
				float b = m1.at<Vec3f>(row, col)[0];
				float g = m1.at<Vec3f>(row, col)[1];
				float r = m1.at<Vec3f>(row, col)[2];

				dest.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(alphe * b + beta);
				dest.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(alphe * g + beta);
				dest.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(alphe * r + beta);
			}
			else if (channels == 1) { //对于单通道
				int pix = image.at<uchar>(row, col);
				dest.at<uchar>(row, col) = saturate_cast<uchar>(alphe * pix + beta);
			}
		}
	}

	Mat imagedst;
	CRect rect;
	GetDlgItem(IDC_PIC_STATIC)->GetClientRect(&rect);
	resize(dest, imagedst, cv::Size(rect.Width(), rect.Height()));
	imshow("view", imagedst);



	/*namedWindow("change");
	imshow("change", dest);
	waitKey();*/
	

}
